package com.ailiwu.www.ailiwu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.activity.PostRecylerActivity;
import com.ailiwu.www.ailiwu.bean.CollectionBean;
import com.ailiwu.www.ailiwu.url.Urls;
import com.ailiwu.www.ailiwu.utils.Logger;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;
import java.util.List;

/**
 * Author:    XiYoung
 * Version    V1.0
 * Date:      2015/12/6 000612:41
 * Description:
 * Modification  History:
 * Date         	Author        		Version        	Description
 * -----------------------------------------------------------------------------------
 * 2015/12/6 0006       XiYoung              1.0                   1.0
 * Why & What is modified:
 */
public class CollectionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<CollectionBean.DataEntity.CollectionsEntity> mDatas = new ArrayList<>();
    private Context mContext;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;

    public CollectionAdapter(Context context, List<CollectionBean.DataEntity.CollectionsEntity> mDatas) {
        this.mDatas = mDatas;
        mContext = context;
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        // 使用DisplayImageOptions.Builder()创建DisplayImageOptions
        options = new DisplayImageOptions.Builder()
                .showImageOnFail(R.drawable.com_taobao_tae_sdk_simple_toast_bg)
                .showImageOnLoading(R.drawable.com_taobao_tae_sdk_simple_toast_bg)
//                .delayBeforeLoading(R.drawable.com_taobao_tae_sdk_simple_toast_bg)
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .displayer(new RoundedBitmapDisplayer(6))   // 设置成圆角图片
                .build();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_recyclerview_home_first_collection, null);
        return new CollectionViewHolder(view);
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        CollectionBean.DataEntity.CollectionsEntity collectionsEntity = mDatas.get(position);

        String url = collectionsEntity.getCover_image_url();
        final String title = collectionsEntity.getTitle();
        String subtitle = collectionsEntity.getSubtitle();
        final int id = collectionsEntity.getId();

        //设置布局
        imageLoader.displayImage(url, ((CollectionViewHolder) holder).ivBg, options);
        ((CollectionViewHolder) holder).tvTitle.setText(title);
        ((CollectionViewHolder) holder).tvSubTitle.setText(subtitle);
        Logger.e("jkajfaklf" + mDatas.toString());
        ((CollectionViewHolder) holder).view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String urlID = Urls.PostsID(id);
                Intent intent = new Intent(mContext, PostRecylerActivity.class);
                intent.putExtra("urlID", urlID);
                intent.putExtra("title", title);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {

        return mDatas == null ? 0 : mDatas.size();
    }


    public static class CollectionViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle, tvSubTitle;
        ImageView ivBg;
        View view;
        LinearLayout llTop;

        public CollectionViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvSubTitle = (TextView) itemView.findViewById(R.id.tv_subtitle);
            ivBg = (ImageView) itemView.findViewById(R.id.iv_image);
            llTop = (LinearLayout) itemView.findViewById(R.id.item_header);
        }
    }

}


